﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSem3.Admin
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        private DataClassesHeaxlthDataContext dc = new DataClassesHeaxlthDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AdminLogin ad = (AdminLogin)Session["admin"];
                if (ad == null)
                {
                    Response.Redirect("login.aspx");

                }
                else
                {
                    if (ad.pemision == 2)
                    {
                        Response.Redirect("login.aspx");
                    }

                }

                
            }
            
        }

        protected void ButtonInsert_Click(object sender, EventArgs e)
        {
            var user = this.dc.EmpRegisters.Where(a => a.username.Equals(this.txtusername.Value)).ToList();
            if (user.Count > 0)
            {
                this.Labelkq.Text = "Username already exists";
            }
            else
            {
                string firstname = this.txtfirstname.Value;
                string lastname = this.txtlastname.Value;
                string username = this.txtusername.Value;
                string password = mahoa_giaima.maHoa(this.txtpassword.Value);
                string retype = mahoa_giaima.maHoa(this.txtretype.Value);
                string joindate = DateTime.Now.ToShortDateString();
                string salary = this.txtsalary.Value.ToString();
                string address = this.txtaddress.Value;
                string contactinfo = this.txtcontactinfo.Value;
                string country = this.txtcountry.Value;
                string city = this.txtcity.Value;
                thememp(firstname, lastname, username, password, retype, joindate, salary, address, contactinfo, country, city);
            }
        }

        public void thememp(string firstname, string lastname, string username, string password, string retype, string joindate, string salary, string address, string contactinfo, string country, string city)
        {
            EmpRegister emp = new EmpRegister();
            emp.firstname = firstname;
            emp.lastname = lastname;
            emp.username = username;
            emp.password = password;
            emp.joindate = Convert.ToDateTime(joindate);
            emp.salary = Convert.ToDecimal(salary);
            emp.address = address;
            emp.contactinfo = contactinfo;
            emp.country = country;
            emp.city = city;
            if (this.txtpassword.Value != this.txtretype.Value)
            {
                this.Labelkq.Text = "The passwords you entered do not match.";
            }
            else
            {
                try
                {
                    this.dc.EmpRegisters.InsertOnSubmit(emp);
                    this.dc.SubmitChanges();
                    this.Labelkq.Text = "Insert successful";
                    Response.Redirect("accountuser.aspx");
                }
                catch
                {
                    this.Labelkq.Text = "Insert failed";
                }
            }
        }
    }
}